import networkx as nx
# import matplotlib.pyplot as plt
import os
import pandas as pd
import Link_Prediction_Scores_All as lp
import pickle
import os
import numpy as np
# import tensorflow as tf


NUM_REPEATS = 2
F_Num_REPEATS = NUM_REPEATS * 1.0
RANDOM_SEED = 0
FRAC_EDGES_HIDDEN = [0.1]
dimen = [0.1]

DEAL_NAME = 'grid_2d-100-100'
combined_dir = 'D:/data-processed/{}-adj.pkl'.format(DEAL_NAME)
with open(combined_dir, 'rb') as f:
    adj,news_edges = pickle.load(f)

test_frac = 0.1
TRAIN_TEST_SPLITS_FOLDER = 'D:/data/splited/ACT13_sample/1_1/{}/'.format(DEAL_NAME)

AUCs = []
pres = []
weak_pres = []

methods = ['Hope','DeepWalk','Node2Vec','SDNE',  'LINE']

for method in methods:
    tmp_AUCs = []
    tmp_pres = []
    tmp_weak_pres = []

    for j in range(NUM_REPEATS):
        print('第{}次执行'.format(j))
        experiment_name = '{}-{}-{}-hidden'.format(DEAL_NAME, test_frac, j)
        train_test_split_file = TRAIN_TEST_SPLITS_FOLDER + experiment_name + '.pkl'

        AUC, pre, weak_pre = lp.calculate_scores(adj, test_frac=test_frac, val_frac=0,
                                                 random_state=RANDOM_SEED, verbose=2,
                                                 train_test_split_file=train_test_split_file, multip=0.6, dims=10, k=2,
                                                 diff=1, method=method)

        tmp_AUCs.append(AUC)
        tmp_pres.append(pre)
        tmp_weak_pres.append(weak_pre)

    tmp_AUC = np.array(tmp_AUCs).sum(axis=0) / F_Num_REPEATS
    tmp_pre = np.array(tmp_pres).sum(axis=0) / F_Num_REPEATS
    tmp_weak_pre = np.array(tmp_weak_pres).sum(axis=0) / F_Num_REPEATS

    AUC = [float('{:.4f}'.format(i)) for i in tmp_AUC]
    pre = [float('{:.4f}'.format(i)) for i in tmp_pre]
    weak_pre = [float('{:.4f}'.format(i)) for i in tmp_weak_pre]

    AUCs.append(AUC)
    pres.append(pre)
    weak_pres.append(weak_pre)


name = ['MLP' , 'Diss']


test = pd.DataFrame(columns=name, data=AUCs)
test.to_csv('{}-All-AUC.csv'.format(DEAL_NAME))

test = pd.DataFrame(columns=name, data=pres)
test.to_csv('{}-All-pre.csv'.format(DEAL_NAME))

test = pd.DataFrame(columns=name, data=weak_pres)
test.to_csv('{}-All-weak_pre.csv'.format(DEAL_NAME))
